package com.liangzhong.model.stadium.stadiumDataManager
{
	import com.common.stdlib.StringUtil;
	import com.common.utils.Debug;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.club.vo.ClubInformation;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.type.MatchType;
	import com.liangzhong.proxy.ProxyUtils;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import lz.controls.Alert;

	/**
	 * 比赛信息Vo 
	 * @author lake
	 * 
	 */	
	public class FriendMatchInfoVo
	{
		public function FriendMatchInfoVo()
		{
		}
		/**
		 *比赛id 
		 */		
		public var matchid :int;
		/**
		 *比赛类型 
		 */		
		public var matchType :int;
		/**
		 *阵容
		 * 1 第一阵容
		 * 2 第二阵容 
		 */		
		public var formation :int;
		/**
		 *俱乐部Id 
		 */
		public var cid:int;
		/**
		 *胜率 
		 */
		public var winrate:int;
		/**
		 * 俱乐部名称
		 */
		public var name:String;
		/**
		 *俱乐部等级 
		 */
		public var  grade:int;
		/**
		 *俱乐部队徽 
		 */
		public var  logo:int;
		/**
		 *自己球服 
		 */
		public var trikot:int;
		/**
		 *球场id 
		 */		
		public var stadiumId :int = 1;
		/**
		 *俱乐部头像 
		 */
		public var headurl2:String; 
		/**
		 *俱乐部天梯等级 
		 */
		public var  level:int;
		/**
		 *俱乐部天梯积分 
		 */
		public var integral:int;
		/**
		 * 俱乐部排名 
		 */
		public var  sort:int;
		/**
		 *俱乐部综合评分 
		 */
		public var score:int; 
		/**
		 *俱乐部正在使用的阵型名称 
		 */
		public var  tname:String;
		/**
		 *俱乐部正在使用的阵型id 
		 */
		public var  tid:int;
		/**
		 *俱乐部正在使用阵型的等级名称 
		 */
		public var  lname:String; 
		/**
		 *所有球员 数据 
		 * MatchPlayerVo
		 */
		public var _players :Array = [];
		/**
		 *场上球员 
		 */		
		public var groundPlayers :Array = [];
		/**
		 *上场球员数据 
		 */
		public var _places:Array = [];
		/**
		 *综合能力 
		 */		
		public var ability	:int;
		/**
		 *进攻 (int)  
		 */
		public var s1:Number = 0;
		/**
		 *控球 (int) 
		 */
		public var s2:Number = 0;
		/**
		 *力量 (int)  
		 */
		public var s3:Number = 0;
		/**
		 *防守 (int)  
		 */
		public var s4:Number = 0;
		/**
		 *抢断 (int)  
		 */
		public var s5:Number = 0;
		/**
		 *速度 (int)  
		 */
		public var s6:Number = 0;
		/**
		 *综合能力 
		 */		
		public var oability :int;
		/**
		 *自己进攻 (int)  
		 */
		public var os1:Number = 0;
		/**
		 *自己控球 (int) 
		 */
		public var os2:Number = 0;
		/**
		 *自己力量 (int)  
		 */
		public var os3:Number = 0;
		/**
		 *自己防守 (int)  
		 */
		public var os4:Number = 0;
		/**
		 *自己抢断 (int)  
		 */
		public var os5:Number = 0;
		/**
		 *自己速度 (int)  
		 */
		public var os6:Number = 0;
		/**
		 *当前球员位置 
		 */
		private var _currentPlayerPlace:Array;
		/**
		 *传奇能力值 固定 
		 */		
		private var LegendAbilitys :Object = {a10104:101,a10103:103,a10102:104,a10101:112,a10100:106,a10099:110,a10019:100};
		/**
		 *解析数据 
		 * @param $obj
		 * 
		 */
		public function analysisData(data:ByteArray):void
		{
			var len :int = 0;
			matchType = data.readByte();
			matchid = data.readUnsignedInt();
			formation = data.readByte();
			oability = data.readShort();
			os1 = data.readShort();
			os4 = data.readShort();
			os5 = data.readShort();
			os3 = data.readShort();
			os6 = data.readShort();
			os2 = data.readShort();
			ability = data.readShort();
			s1 = data.readShort();
			s4 = data.readShort();
			s5 = data.readShort();
			s3 = data.readShort();
			s6 = data.readShort();
			s2 = data.readShort();
			cid = data.readInt();
			if(matchType == MatchType.LEGEND_MATCH)
			{
				if(LegendAbilitys.hasOwnProperty("a" + cid))
				{
					ability = int(LegendAbilitys["a" + cid]);
				}
			}
			len = data.readShort();
			if(len > 0)
			{
				headurl2 = StringUtil.readStringByByteArray(data,len);
			}
			len = data.readShort();
			if(len > 0)
			{
				name = StringUtil.readStringByByteArray(data,len);
			}
			grade = data.readShort();
			logo = data.readShort();
			tid = data.readByte();
			trikot = data.readInt();
			stadiumId = data.readInt();
			len = data.readShort();
			_players = [];
			_places = [];
			groundPlayers = [];
			var playerPlace:PlayerPlaceVo;
			var playerVo:MatchPlayerVo;
			var i:int;
			var playerStar :PlayerStarVo;
			var starId	   :int;
			for(i = 0 ;i < len;i++)
			{
				playerVo = new MatchPlayerVo();
				playerVo.id = data.readInt();
				starId = data.readInt();
				playerVo.starid = starId;
				playerStar = AppData.getInstance().goodsManager.getStarById(starId) as PlayerStarVo;
				playerVo.grade = data.readShort();
				playerVo.level = playerVo.grade;
				playerVo.number = data.readByte();
				if(starId!=0)
				{
					playerVo.headurl2 = playerStar==null?"":playerStar.url;
					playerVo.pname = playerStar==null?"无名":playerStar.name;
					playerVo.name = playerStar==null?"无名":playerStar.name;
					playerVo.ability = playerStar==null?60:playerStar.ability;
				}
				else
				{
					playerVo.headurl2 = headurl2;
					playerVo.pname = name;
					playerVo.name = name;
				}
				playerVo.color = data.readByte();
				playerVo.placeIndex = data.readByte();
				playerVo.s1 = data.readShort();
				playerVo.s4 = data.readShort();
				playerVo.s5 = data.readShort();
				playerVo.s3 = data.readShort();
				playerVo.s6 = data.readShort();
				playerVo.s2 = data.readShort();
				playerVo.fx = data.readShort();
				playerVo.fy = data.readShort();
				playerVo.attack 	= playerVo.s1;
				playerVo.defence 	= playerVo.s4;
				playerVo.control 	= playerVo.s2;
				playerVo.steal 		= playerVo.s5;
				playerVo.power 		= playerVo.s3;
				playerVo.speed 		= playerVo.s6;
				_players.push(playerVo);
				
				playerPlace = new PlayerPlaceVo();
				playerPlace.pid = playerVo.id;
				playerPlace.place = playerVo.tplace;
				playerPlace.fx = playerVo.fx;
				playerPlace.fy = playerVo.fy;
				playerVo.analysisPlaceRes();
				groundPlayers.push(playerVo);
				_places.push(playerPlace);
			}
			groundPlayers.sortOn("color",Array.DESCENDING);
		}
		/**
		 *解析自己 
		 */		
		public function parseSelf(matchId:int):void
		{
			var clubInfo :ClubInformation = AppData.getInstance().clubDatamanager.clubInfo;
			cid = clubInfo.clubid;
			name = clubInfo.uname;
			trikot = clubInfo.homekot;
			headurl2 = clubInfo.header;
			logo = clubInfo.logo;
			grade = clubInfo.level;
			ability = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].oability;
			s1 = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].os1;
			s2 = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].os2;
			s3 = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].os3;
			s4 = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].os4;
			s5 = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].os5;
			s6 = AppData.getInstance().stadiumDataManager.opponentMatchInfo[matchId].os6;
			_players = [];
			_places = [];
			groundPlayers = [];
			var playerPlace:PlayerPlaceVo;
			var playerVo:MatchPlayerVo;
			var i:int;
			var starId	   :int;
			var len :int = 11;
			var battlePlayerList :Array = AppData.getInstance().playerCenterDataManager.firstBattlearrayVo.battlearrayList;
			var ppvo:*;
			var plyvo :PlayerVo;
			for(i = 0 ;i < len;i++)
			{
				ppvo = battlePlayerList[i];
				plyvo = AppData.getInstance().playerCenterDataManager.getplayerVoById(ppvo.playerid);
				playerVo = new MatchPlayerVo();
				playerVo.id = plyvo.playerid;
				starId = plyvo.starid;
				playerVo.starid = starId;
				playerVo.self = plyvo.self;
				playerVo.grade = plyvo.level;
				playerVo.headurl2 = plyvo.url;
				playerVo.number = plyvo.number;
				playerVo.level = plyvo.level;
				playerVo.ability = plyvo.ability;
				if(plyvo.starVo)
				{
					playerVo.pname = plyvo.starVo.name;
					playerVo.name = plyvo.starVo.name;
				}
				else
				{
					playerVo.pname = "无名";
					playerVo.name = "无名";
				}
				playerVo.color = plyvo.color;
				playerVo.placeIndex = plyvo.place;
				playerVo.s1 = plyvo.attack;
				playerVo.s4 = plyvo.defence;
				playerVo.s5 = plyvo.steal;
				playerVo.s3 = plyvo.power;
				playerVo.s6 = plyvo.speed;
				playerVo.s2 = plyvo.control;
				playerVo.fx = ppvo.flasx;
				playerVo.fy = ppvo.flasy;
				playerVo.attack 	= playerVo.s1;
				playerVo.defence 	= playerVo.s4;
				playerVo.control 	= playerVo.s2;
				playerVo.steal 		= playerVo.s5;
				playerVo.power 		= playerVo.s3;
				playerVo.speed 		= playerVo.s6;
				_players.push(playerVo);
				
				playerPlace = new PlayerPlaceVo();
				playerPlace.pid = playerVo.id;
				playerPlace.place = playerVo.tplace;
				playerPlace.fx = playerVo.fx;
				playerPlace.fy = playerVo.fy;
				playerVo.analysisPlaceRes();
				groundPlayers.push(playerVo);
				_places.push(playerPlace);
			}
			groundPlayers.sortOn("color",Array.DESCENDING);
		}
		/**
		 * 获得球员姓名 
		 * @param $id
		 * @return 
		 * 
		 */
		public function getPlayerNameById($id:int):String
		{
			var playerVo:MatchPlayerVo = getMatchPlayerById($id);
			if(playerVo == null)return "";
			return playerVo.pname;
		}
		/**
		 * 
		 * @param pid
		 * 
		 */		
		public function getMatchPlayerById(pid :int):MatchPlayerVo
		{
			for each(var vo :MatchPlayerVo in _players)
			{
				if(vo.id == pid)
				{
					return vo;
				}
			}
			return null;
		}
		/**
		 *获取球员位置 
		 * @return 
		 * 
		 */		
		public function getPlayerPlaceById($id:int):String
		{
			for each(var pp :PlayerPlaceVo in _places)
			{
				if(pp.pid == $id)
				{
					return pp.place;
				}
			}
			return "";
		}
		/**
		 *解析记录中数据
		 * @param obj
		 */		
		public function parseHistoryData(obj :Object):void
		{
			for(var str:String in obj)
			{
				if(this.hasOwnProperty(str))
				{
					this[str] = obj[str];
				}
			}
			var tempPlayreObjs :Array = _players;
			_players = [];
			var playerVo :MatchPlayerVo;
			for each(var playerObj :Object in tempPlayreObjs)
			{
				playerVo = new MatchPlayerVo();
				playerVo.parseHistoryData(playerObj);
				_players.push(playerVo);
			}
			var tempPlaceObjs :Array = _places;
			_places = [];
			var placeVo :PlayerPlaceVo;
			groundPlayers = [];
			for each(var placeObj :Object in tempPlaceObjs)
			{
				placeVo = new PlayerPlaceVo();
				placeVo.parseHistoryData(placeObj);
				_places.push(placeVo);
				playerVo = getMatchPlayerById(placeVo.pid);
				if(playerVo)
				{
					playerVo.fx = placeVo.fx;
					playerVo.fy = placeVo.fy;
					playerVo.place = 0;
					playerVo.tplace = "";
					groundPlayers.push(playerVo);
					placeVo.headurl = playerVo.url;
				}
			}
		}
	}
}